package server;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.Socket;

import common.ClientInterface;
import common.ComandiException;
import common.SocketPerClientInterface;


public class ServerSocketProxy extends Thread{

	 private static int counter = 0;
	 private int id = ++counter;

	 private Socket socket;
	 private BufferedReader bufferedReader;
	 private ClientInterface clientInt;
	 private Giocatore giocatore;
	 private String token;
	 private Partita partita;
	 
	 public ServerSocketProxy(Server server,Socket socket) throws IOException {
	    this.socket=socket;
	    clientInt = new SocketPerClientInterface(socket);
	    bufferedReader= new BufferedReader(new InputStreamReader(socket.getInputStream()));
	    partita=server.getPartita();
	 }
	 
	 public void run(){
		 try{
			 while(true){
				 //ascolta messaggi
				 String mess=bufferedReader.readLine();
				 riconoscimento(mess);
			}
		}catch(IOException e){} 
			try {
			  socket.close();
			}catch(IOException e){}
		}


	private void riconoscimento(String mess){
		
		String[] partiMess=mess.split("\\,");
		
		////----------------------------------RICHIESTA CREA UTENTE----------------------------------------
		if(partiMess[0].equals("@creaUtente")){
			String user= partiMess[1].split("\\=")[1];
			String pass= partiMess[2].split("\\=")[1];
			try {
				partita.creaUtente(user, pass);
				clientInt.ok();
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		////--------------------------------------RICHIESTA LOGIN--------------------------------------------
		else if(partiMess[0].equals("@login")){
			String user= partiMess[1].split("\\=")[1];
			String pass= partiMess[2].split("\\=")[1];
			try {
				String token=partita.login(user,pass,null);
				clientInt.ok(token);
				this.token=token;
				for(Giocatore g: partita.getGiocatoriNonInPartita()){
					if(g.getToken()==token){
						giocatore=g;
						giocatore.setClientInt(clientInt);
					}
				}
				
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
		////----------------------------------RICHIESTA CREA RAZZA----------------------------------------
		else if(partiMess[0].equals("@creaRazza")){
			String token= partiMess[1].split("\\=")[1];
			String nome= partiMess[2].split("\\=")[1];
			char tipo= partiMess[3].split("\\=")[1].toCharArray()[0];
			try {
				partita.creaRazza(token, nome, tipo);
				clientInt.ok();
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	////----------------------------------RICHIESTA ACCESSO PARTITA--------------------------------
		else if(partiMess[0].equals("@accessoPartita")){
			String token= partiMess[1].split("\\=")[1];
			
			try {
				partita.accessoPartita(token);
				clientInt.ok();
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	////----------------------------------RICHIESTA USCITA PARTITA--------------------------------
		else if(partiMess[0].equals("@uscitaPartita")){
			String token= partiMess[1].split("\\=")[1];
			
			try {
				partita.uscitaPartita(token);
				clientInt.ok();
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	////----------------------------------RICHIESTA LISTA GIOCATORI--------------------------------
		else if(partiMess[0].equals("@listaGiocatori")){
			String token= partiMess[1].split("\\=")[1];
			
			try {
				String[] lista=partita.listaGiocatori(token);
				clientInt.listaGiocatori(lista);
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
	
	////----------------------------------RICHIESTA CLASSIFICA----------------------------------------
		else if(partiMess[0].equals("@classifica")){
			String token= partiMess[1].split("\\=")[1];
		
			try {
				String[] classifica=partita.classifica(token);
				clientInt.classifica(classifica);
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	
	////--------------------------------------RICHIESTA LOGOUT	--------------------------------------------
		else if(partiMess[0].equals("@logout")){
			String token= partiMess[1].split("\\=")[1];
			
			try {
				partita.logout(token);
				clientInt.ok(token);
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}		
		
	////----------------------------------RICHIESTA MAPPA GENERALE----------------------------------------
		else if(partiMess[0].equals("@mappaGenerale")){
			String token= partiMess[1].split("\\=")[1];
		
			try {
				String[] mappa=partita.mappaGenerale(token);
				clientInt.mappaGenerale(mappa);
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	////----------------------------------LISTA DINOSAURI----------------------------------------
		else if(partiMess[0].equals("@listaDinosauri")){
			String token= partiMess[1].split("\\=")[1];
					
			try {
				String[] lista=partita.listaDinosauri(token);
				clientInt.listaDinosauri(lista);
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	////----------------------------------VISTA LOCALE----------------------------------------
		else if(partiMess[0].equals("@vistaLocale")){
			String token= partiMess[1].split("\\=")[1];
			String idDino= partiMess[2].split("\\=")[1];
		
			try {
				String[] vista=partita.vistaLocale(token,idDino);
				clientInt.vistaLocale(vista);
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	////----------------------------------STATO DINOSAURO----------------------------------------
		else if(partiMess[0].equals("@statoDinosauro")){
			String token= partiMess[1].split("\\=")[1];
			String idDino= partiMess[2].split("\\=")[1];
		
			try {
				String stato=partita.statoDinosauro(token,idDino);
				clientInt.statoDinosauro(stato);
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
	////----------------------------------MOVIMENTO----------------------------------------
		else if(partiMess[0].equals("@muoviDinosauro")){
			String token= partiMess[1].split("\\=")[1];
			String idDino= partiMess[2].split("\\=")[1];
			String destx= partiMess[3].split("\\=")[1].split("\\{")[1]; //{x
			String desty=partiMess[4].split("\\}")[0];//y}
			
			String x=destx;
			String y=desty;
			int xInt=Integer.parseInt(x); 
			int yInt=Integer.parseInt(y);	
					
			try {
				String risp=partita.movimento(token,idDino,xInt,yInt);
				if(risp.equals("ok"))
					clientInt.ok();
				else if(risp.equals("ok,v"))
					clientInt.ok("v");
				else
					clientInt.ok("p");
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	////----------------------------------CRESCITA----------------------------------------
		else if(partiMess[0].equals("@cresciDinosauro")){
			String token= partiMess[1].split("\\=")[1];
			String idDino= partiMess[2].split("\\=")[1];
		
			try {
				String risp=partita.crescita(token,idDino);
				clientInt.ok();
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	////----------------------------------DEPOSIZIONE UOVO----------------------------------------
		else if(partiMess[0].equals("@deponiUovo")){
			String token= partiMess[1].split("\\=")[1];
			String idDino= partiMess[2].split("\\=")[1];
			
			try {
				String idNato=partita.deposizioneUovo(token, idDino);
				clientInt.ok(idNato);
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	////----------------------------------CONFERMA TURNO----------------------------------------
		else if(partiMess[0].equals("@confermaTurno")){
			String token= partiMess[1].split("\\=")[1];
		
			try {
				partita.confermaTurno(token);
				clientInt.ok();
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	////----------------------------------PASSA TURNO----------------------------------------
		else if(partiMess[0].equals("@passaTurno")){
			String token= partiMess[1].split("\\=")[1];
		
			try {
				partita.passaTurno(token);
				clientInt.ok();
			} catch (ComandiException e) {
				try {
					clientInt.no(e.getMessage());
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			catch (IOException e) {
				try {
					clientInt.no("errore di comunicazione");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	}

}